<?
class sym_light_menu_admin extends symbiont{
    public function ___main($design=''){
        global $kernel, $sql, $sym;
        if($kernel->link->param2){
            $design=$this->_check($design, 'admin_items', 'light_menu');
            $d=new design($design, 'light_menu', false);
            $menu_name=$kernel->link->param2;
            $sym->light_menu->content='';
            $sym->light_menu->menu_id=$menu_id=$sql->select_each("sym_light_menu", 'id', array('name'=>$menu_name), '', '', 1);
            $sym->light_menu->items='';
            $sym->light_menu->get_handmade($menu_id);
            $d->inc();
        }
        else{
            $design=$this->_check($design, 'admin', 'light_menu');
            $d=new design($design, 'light_menu_admin', false);
            $this->items='';
            $menus=$sql->select_each('sym_light_menu', array('name', 'id'), '', 'id');
            if(is_array($menus))
            foreach($menus as $menu){
                $this->id=$menu['id'];
                $this->name=$menu['name'];
                $this->items.=design::run($this->item);
            }
            $d->inc();
        }
    }
    //Menu adding
    public function ___add($design=''){
        $design=$this->_check($design, 'add', 'light_menu');
        $d=new design($design, 'light_menu_admin', false);
        $this->name='';
        $d->inc();
    }
    //Menu editing
    public function ___edit($design='', $id=''){
        global $sql;
        $design=$this->_check($design, 'add', 'light_menu');
        $id=data::number($id);
        if($id=='') return;
        $name=$sql->select_each('sym_light_menu', 'name', array('id'=>$id), '', '', 1);
        $d=new design($design, 'light_menu_admin', false);
        $this->name=$name;
        $d->inc();
    }
    //Menu deleteing
    public function ___delete($design=''){
        $design=$this->_check($design, 'delete', 'light_menu');
        $d=new design($design, 'light_menu_admin');
    }
    
    //Menu item adding
    public function ___item_add($design='', $menu_id){
        global $sql;
        $design=$this->_check($design, 'item_add', 'light_menu');
        $d=new design($design, 'light_menu_admin', false);
        
        $this->page_checked='checked="checked"';
        $this->link_checked='';
        $this->page_disabled='';
        $this->link_disabled='disabled="disabled"';
        $this->val_link='';
        $this->val_parent=0;
        $this->val='';
        
        $this->infos='';
        $this->languages='';
        $languages=$sql->select_each('languages', array('abbr', 'id'), array('is_enabled'=>1), '', 'position');
        foreach($languages as $val){
            $this->id=$val['id'];
            $this->abbr=$val['abbr'];
            $this->languages.=design::run($this->language);
            $this->infos.=design::run($this->info);
        }
        $this->pages='';
        $this->pages(0, '', 246, 168, 40);
        $this->menu_id=$menu_id;
        $this->handmade='';
        $this->handmade(0, '', 246, 168, 40);
        $d->inc();
    }
    //Menu item adding
    public function ___item_edit($design='', $menu_id, $id){
        if(!data::is_uint($menu_id)||!data::is_uint($id)) return;
        global $sql;
        $design=$this->_check($design, 'item_add', 'light_menu');
        $d=new design($design, 'light_menu_admin', false);
        $item=$sql->query_each('SELECT h.link, h.page_id, h.menu_id, h.title_lid, h.parent_id
                         FROM `sym_light_menu_handmade` as h
                         WHERE h.menu_id = '.$menu_id.' && h.id='.$id.'
                         LIMIT 1', true);
        if(!is_array($item)) return;
        $this->link_checked=$this->page_checked=$this->page_disabled=$this->link_disabled='';
        $this->val_link=$item['link'];
        if($item['link']){
            $this->link_checked='checked="checked"';
            $this->page_disabled='disabled="disabled"';
        }
        else{
            $this->page_checked='checked="checked"';
            $this->link_disabled='disabled="disabled"';
        }
        
        $this->infos='';
        $this->languages='';
        $languages=$sql->select_each('languages', array('abbr', 'id'), array('is_enabled'=>1), '', 'position');
        $l=new label($item['title_lid']);
        foreach($languages as $val){
            $this->id=$val['id'];
            $this->abbr=$val['abbr'];
            $this->val=$l->get($val['id']);
            $this->languages.=design::run($this->language);
            $this->infos.=design::run($this->info);
        }
        $this->val_parent=$item['page_id'];
        $this->pages='';
        $this->pages(0, '', 246, 168, 40);
        $this->menu_id=$menu_id;
        $this->handmade='';
        $this->val_parent=$item['parent_id'];
        $this->handmade(0, '', 246, 168, 40);
        $d->inc();
    }
    
    //Menu item deleteing
    public function ___item_delete($design='', $id=''){        
        $design=$this->_check($design, 'item_delete', 'light_menu');
        $d=new design($design, 'light_menu_admin');
    }
    
    //Menu adding in database
    public function ___db_add($design=''){
        global $sql, $user;
        if($user->access_level<9){
            print('{"error":"You have not enought prerogatives."}');
            return;
        }
        if(!isset($_POST['name'])){
            print('{"error":"Not the all parametrs have been sent."}');
            return;
        }
        $name=data::word($_POST['name']);
        $sql->insert('sym_light_menu', array('name'=>$name));
        print('{"ok":"Menu added."}');
    }
    //Menu edition
    public function ___db_edit($design=''){
        global $sql, $user;
        if($user->access_level<9){
            print('{"error":"You have not enought prerogatives."}');
            return;
        }
        if(!isset($_POST['name'])||!isset($_POST['id'])){
            print('{"error":"Not the all parametrs have been sent."}');
            return;
        }
        $id=data::word($_POST['id']);
        $name=data::word($_POST['name']);
        $sql->insert('sym_light_menu', array('name'=>$name), array('id'=>$id));
        print('{"ok":"Menu edited."}');
    }
    //Menu deleteing
    public function ___db_delete($design=''){
        global $sql, $user;
        if($user->access_level<9){
            print('{"error":"You have not enought prerogatives."}');
            return;
        }
        if(!isset($_POST['id'])){
            print('{"error":"Not the all parametrs have been sent."}');
            return;
        }
        $id=$_POST['id'];
        if(!data::is_uint($id)){
            print('{"error":"Id most be unsigned int."}');
            return;
        }
        
        $sql->delete('sym_light_menu_handmade', array('menu_id'=>$id));
        $sql->delete('sym_light_menu', array('id'=>$id), 1);
        print('{"ok":"Menu deleted."}');
    }
    
    //Menu item adding in database
    public function ___db_item_add($design=''){
        global $sql, $user;
        if($user->access_level<9){
            print('{"error":"You have not enought prerogatives."}');
            return;
        }
        if((!isset($_POST['page_id'])&&!isset($_POST['link']))||!isset($_POST['menu_id'])||!isset($_POST['parent_id'])){
            print('{"error":"Not the all parametrs have been sent."}');
            return;
        }
        if(isset($_POST['page_id'])){
            if(!data::is_uint($_POST['page_id'])){
                print('{"error":"Incorrect parametr type."}');
                return;
            }
            $page_id=$_POST['page_id'];
            if($page_id==0){
                print('{"error":"Select menu item."}');
                return;
            }
        }
        else{
            $page_id=0;
        }
        if(isset($_POST['link'])){
            $link=data::safe($_POST['link']);
        }
        else{
            $link='';
        }
        
        if(!data::is_uint($_POST['menu_id'])||!data::is_uint($_POST['parent_id'])){
            print('{"error":"Incorrect parametr type."}');
            return;
        }
        
        $menu_id=$_POST['menu_id'];
        $parent_id=$_POST['parent_id'];
        
        $title_lid=0;
        if(isset($_POST['title'])){
            $label_name='symbionts.light_menu.items.'.data::uniq(6);
            $l=new label($label_name);
            foreach($_POST['title'] as $key=>$val){
                $val=data::safe($val);
                $key=data::word($key, 'en', 2);
                $l->add($val, $key);
            }
            $title_lid=$l->id;
        }
        $r=$sql->insert('sym_light_menu_handmade', array('page_id'=>$page_id, 'menu_id'=>$menu_id, 'link'=>$link, 'title_lid'=>$title_lid, 'parent_id'=>$parent_id));
        if($r){
            $label_name='symbionts.light_menu.items.'.$r;
            $sql->update('labels', array('name'=>$label_name), array('id'=>$title_lid), 1);
            $sql->update('sym_light_menu_handmade', array('position'=>$r), array('id'=>$r), 1);
            data::folder_delete('cache/light_menu/handmade/'.$menu_id.'/');
            print('{"ok":"Menu item added."}');
        }
        else{
            $l->del();
            print('{"error":"Adding error."}');
        }
    }
    
    //Menu item editiong
    public function ___db_item_edit($design=''){
        global $sql, $user;
        if($user->access_level<9){
            print('{"error":"You have not enought prerogatives."}');
            return;
        }
        if((!isset($_POST['page_id'])&&!isset($_POST['link']))||!isset($_POST['menu_id'])||!isset($_POST['parent_id'])||!isset($_POST['id'])){
            print('{"error":"Not the all parametrs have been sent."}');
            return;
        }
        if(isset($_POST['page_id'])){
            if(!data::is_uint($_POST['page_id'])){
                print('{"error":"Incorrect parametr type."}');
                return;
            }
            $page_id=$_POST['page_id'];
            if($page_id==0){
                print('{"error":"Select menu item."}');
                return;
            }
        }
        else{
            $page_id=0;
        }
        
        if(isset($_POST['link'])){
            $link=data::safe($_POST['link']);
        }
        else{
            $link='';
        }
        
        if(!data::is_uint($_POST['menu_id'])||!data::is_uint($_POST['parent_id'])||!data::is_uint($_POST['id'])){
            print('{"error":"Incorrect parametr type."}');
            return;
        }
        
        $menu_id=$_POST['menu_id'];
        $parent_id=$_POST['parent_id'];
        $id=$_POST['id'];
        
        $title_lid=0;
        if(isset($_POST['title'])){
            $label_name='symbionts.light_menu.items.'.$id;
            $l=new label($label_name);
            foreach($_POST['title'] as $key=>$val){
                $val=data::safe($val);
                $key=data::word($key, 'en', 2);
                $l->set($val, $key);
            }
            $title_lid=$l->id;
        }
        $r=$sql->update('sym_light_menu_handmade', array('page_id'=>$page_id, 'menu_id'=>$menu_id, 'link'=>$link, 'title_lid'=>$title_lid, 'parent_id'=>$parent_id), array('id'=>$id));
        if($r){
            data::folder_delete('cache/light_menu/handmade/'.$menu_id.'/');
            print('{"ok":"Menu item edited."}');
        }
        else{
            print('{"error":"Editing error."}');
        }
    }
    
    //Menu deleteing
    public function ___db_item_delete($design=''){
        global  $user;
        if($user->access_level<9){
            print('{"error":"You have not enought prerogatives."}');
            return;
        }
        if(!isset($_POST['id'])||!isset($_POST['menu_id'])){
            print('{"error":"Not the all parametrs have been sent."}');
            return;
        }
        if(!data::is_uint($_POST['id'])||!data::is_uint($_POST['menu_id'])){
            print('{"error":"Incorrect parametr type."}');
            return;
        }
        $id=$_POST['id'];
        $menu_id=$_POST['menu_id'];
        if($this->item_delete($id)){
            data::folder_delete('cache/light_menu/handmade/'.$menu_id.'/');
            print('{"ok":"Menu deleted."}');
        }
        else print('{"error": "Deleteing error."}');
    }
    private function item_delete($id){
        global $sql;
        $title_lid=$sql->select_each('sym_light_menu_handmade', 'title_lid', array('id'=>$id), '', '', 1);
        
        $l=new label($title_lid);
        $l->del();
        
        $sub=$sql->select_each('sym_light_menu_handmade', 'id', array('parent_id'=>$id));
        if($sub){
            foreach($sub as $val){
                $this->item_delete($val['id']);
            }
        }
        return $sql->delete('sym_light_menu_handmade', array('id'=>$id), 1);
    }
    
    //Sort items
    public function ___db_sort($design=''){
        global $user, $sql;
        if($user->access_level<9){
            print('{"error":"You have not enought prerogatives."}');
            return;
        }
        if(!isset($_POST['sort'])||!isset($_POST['menu_id'])){
            print('{"error":"Not the all parametrs have been sent."}');
            return;
        }
        if(!data::is_uint($_POST['menu_id'])){
            print('{"error":"Incorrect parametr type."}');
            return;
        }
        foreach($_POST['sort'] as $key=>$val){
            $id=data::number($val);
            $position=data::number($key)+1;
            $sql->update('sym_light_menu_handmade', array('position'=>$position), array('id'=>$id), 1);
        }
        data::folder_delete('cache/light_menu/handmade/'.$menu_id.'/');
    }
    
    //Private functions
    private function pages($id=0, $prefix='', $r=0, $g=0, $b=0, $s=true){
        if($r<=0&&$g<=0&&$b<=0){
            $r=rand(200, 255);
            $g=rand(200, 255);
            $b=rand(200, 255);
        }
        global $sql, $kernel;
        $pages=$sql->query_each("SELECT p.id, p.name, l.value as title
                                FROM `pages` as p LEFT JOIN `labels` as l ON p.title_lid=l.id AND l.lng_id = '".$kernel->lang->id."'
                                WHERE p.parent_id = '$id' AND p.is_active = 1
                                ORDER BY `name`");
        if(is_array($pages)){
            foreach($pages as $key=>$val){
                if($val['name'][0]=='$') continue;
                $this->background='#'.$this->hex($r).$this->hex($g).$this->hex($b);
                $this->page_title=$prefix.($val['title']?$val['title']:$val['name']);
                $this->page_id=$val['id'];
                if($this->val_parent!=''&&$this->val_parent>=0) $this->selected=$val['id']==$this->val_parent?'selected="selected"':'';
                else $this->selected='';
                $this->pages.=design::run($this->temp);
                if($this->page_id!=0){
                    $this->pages($this->page_id, '&nbsp;'.$prefix, $r-10>0?$r-10:0, $g-10>0?$g-10:0, $b-10>0?$b-10:0, $s);
                }
            }
        }
    }
    private function handmade($id=0, $prefix='', $r=0, $g=0, $b=0, $s=true){
        if($r<=0&&$g<=0&&$b<=0){
            $r=rand(200, 255);
            $g=rand(200, 255);
            $b=rand(200, 255);
        }
        global $sql, $kernel;
        $pages=$sql->query_each("SELECT h.id, p.name, l.value as title, h.link
                                FROM `sym_light_menu_handmade` as h LEFT JOIN `pages` as p ON h.page_id=p.id LEFT JOIN `labels` as l ON p.title_lid=l.id AND l.lng_id = ".$kernel->lang->id."
                                WHERE h.parent_id = ".$id." AND h.menu_id=".$this->menu_id."
                                ORDER BY h.position");
        if(is_array($pages)){
            foreach($pages as $key=>$val){
                if($val['name'][0]=='$') continue;
                $this->background='#'.$this->hex($r).$this->hex($g).$this->hex($b);
                $this->handmade_name=$prefix.($val['title']?$val['title']:$val['name']);
                $this->handmade_id=$val['id'];
                if($this->val_parent!=''&&$this->val_parent>=0) $this->selected=$val['id']==$this->val_parent?'selected="selected"':'';
                else $this->selected='';
                $this->handmade.=design::run($this->temp_handmade);
                if($this->handmade_id!=0){
                    $this->handmade($this->handmade_id, '&nbsp;'.$prefix, $r-10>0?$r-10:0, $g-10>0?$g-10:0, $b-10>0?$b-10:0, $s);
                }
            }
        }
    }
    private function hex($dec){
        $hex=dechex($dec);
        if(strlen($hex)==0) return '00';
        if(strlen($hex)==1) return '0'.$hex;
        return $hex;
    }
}
?>